package com.judicial.app.utils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.UUID;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import com.judicial.app.R;

@SuppressLint("SimpleDateFormat") public class StringUtil {

	/**
	 * 设置价格字体（红色，变大，粗体，删除线） 如果不设�???，将�???��位置�???1
	 * 
	 * @param context
	 * @param str
	 *            源字符串
	 * @param start0
	 *            设置字体大小�???���???
	 * @param end0
	 *            设置字体大小结束�???
	 * @param start1
	 *            设置字体粗体�???���???
	 * @param end1
	 *            设置字体粗体结束�???
	 * @param start2
	 *            设置删除线开始位
	 * @param end2
	 *            设置删除线结束位
	 * @param start3
	 *            设置字体红色�???���???
	 * @param end3
	 *            设置字体红色结束�???
	 * @return
	 */
	public static SpannableString buildSpecialStr(Context context, String str,
			int start0, int end0, int start1, int end1, int start2, int end2,
			int start3, int end3) {
		SpannableString msp = null;
		if (!TextUtils.isEmpty(str)) {
			msp = new SpannableString(str);
			if (start0 != -1) {
				// 設置字体变大
				msp.setSpan(new RelativeSizeSpan(1.3f), start0, end0,
						Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 2.0f表示默认字体大小的两�???
			}
			if (start1 != -1) {
				// 设置用粗体显�???
				msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),
						start1, end1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			}
			if (start2 != -1) {
				// 删除�???
				msp.setSpan(new StrikethroughSpan(), start2, end2,
						Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			}
			if (start3 != -1) {
				// 设置字体颜色(前景�???
				// msp.setSpan(new
				// ForegroundColorSpan(context.getResources().getColor(R.color.gbfcolor2)),
				// start3, end3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			}
			return msp;
		} else {
			return null;
		}
	}

	/**
	 * 根据指定日期获取utc时间
	 * 
	 * @param date
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static long getUTCDateBySpecifiedDateString(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date dateToUtc = sdf.parse(date);
			Calendar cal = Calendar.getInstance();
			cal.setTime(dateToUtc);
			return cal.getTimeInMillis();
		} catch (ParseException e) {
			e.printStackTrace();
			return 0;
		}

	}

	public static SpannableString buildSpecialStr(Context context, String str) {
		SpannableString msp = null;
		if (!TextUtils.isEmpty(str)) {
			msp = new SpannableString(str);
			int size = str.length();
			// 删除�???
			msp.setSpan(new StrikethroughSpan(), 0, size - 1,
					Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			return msp;
		} else {
			return null;
		}
	}

	/**
	 * 商品详情使用
	 * 
	 * @param resouce
	 * @return
	 */
	public static String getOneFloatNum(String resouce) {
		String result = null;
		if (resouce != null) {
			int pointPosition = resouce.indexOf(".");
			if (pointPosition == -1) {
				// 没有小数�???
				result = resouce + ".0";
			} else {
				result = resouce.substring(0, pointPosition + 2);
			}
		}
		return result;
	}

	/**
	 * 根据utc秒数获取指定日期格式
	 * 
	 * @param utcStandardString
	 *            注：参数utcStandard 为秒�???
	 * @param type
	 *            长日�???4小时制type = 1 2014-9-5 15:45:02 长日�???2小时制type = 2 2014-9-5
	 *            03:45:02 短日期格式type = 3 2014-9-5
	 * @return
	 */
	public static String getDateFomatByType(String utcStandardString, int type) {
		SimpleDateFormat format = null;
		if (type == 1) {
			format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		} else if (type == 2) {
			format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CHINA);
		} else if (type == 3) {
			format = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
		}

		Calendar cal = Calendar.getInstance();
		// 取得时间偏移量： 东八区：28800000 = 8*60*60*1000 ms
		final int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
		try {
			long utcStandard = Long.parseLong(utcStandardString);
			return format.format(utcStandard * 1000 + zoneOffset);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			// 1970-01-01 08:00:00
			return format.format(0);
		}
	}

	/**
	 * 根据utc秒数获取指定日期格式
	 * 
	 * @param utcStandard
	 *            注：参数utcStandard 为秒�???
	 * @param type
	 *            长日�???4小时制type = 1 2014-9-5 15:45:02 长日�???2小时制type = 2 2014-9-5
	 *            03:45:02 短日期格式type = 3 2014-9-5
	 * @return
	 */
	public static String longTime2FormatDate(long utcStandard, int type) {
		SimpleDateFormat format = null;
		if (type == 1) {
			format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		} else if (type == 2) {
			format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CHINA);
		} else if (type == 3) {
			format = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
		}

		Calendar cal = Calendar.getInstance();
		// 取得时间偏移量： 东八区：28800000 = 8*60*60*1000 ms
		final int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
		try {
			return format.format(utcStandard * 1000 + zoneOffset);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			// 1970-01-01 08:00:00
			return format.format(0);
		}
	}

	/**
	 * 获取UUID
	 * 
	 * @return
	 */

	public static String uuid() {
		return UUID.randomUUID().toString();
	}

	/**
	 * 获取当前日期格式化的字符�???yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static String getCurrentDateStringss() {
		// 使用默认时区和语�???��境获得一个日�???
		Calendar cale = Calendar.getInstance();
		// 将Calendar类型转换成Date类型
		Date now = cale.getTime();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String timeStr = format.format(now);
		return timeStr;
	}

	/**
	 * 获取当前日期格式化的字符�???yyyy-MM-dd
	 * 
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static String getCurrentShortDateString() {
		// 使用默认时区和语�???��境获得一个日�???
		Calendar cale = Calendar.getInstance();
		// 将Calendar类型转换成Date类型
		Date now = cale.getTime();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String timeStr = format.format(now);
		return timeStr;
	}

	/**
	 * 根据指定日期，返回格式化日期字符�???
	 * 
	 * @param year
	 * @param month
	 * @param day_of_month
	 * @param hour_of_day
	 * @param minite
	 * @param second
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static String getSpecifiedDate(int year, int month,
			int day_of_month, int hour_of_day, int minite, int second) {
		// 使用默认时区和语�???��境获得一个日�???
		Calendar cale = Calendar.getInstance();
		// 对日期和时间进行设置
		cale.set(Calendar.YEAR, year);
		// 此处注意，MONTH是从0-11,0表示1月，1表示2月，依次类推
		cale.set(Calendar.MONTH, month);
		cale.set(Calendar.DAY_OF_MONTH, day_of_month);
		cale.set(Calendar.HOUR_OF_DAY, hour_of_day);
		cale.set(Calendar.MINUTE, minite);
		cale.set(Calendar.SECOND, second);
		// 将Calendar类型转换成Date类型
		Date tasktime = cale.getTime();
		// 设置日期输出的格�???
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// 格式化输�???
		return df.format(tasktime);
	}

	public static Boolean isNullOrEmpty(String str) {
		if (null != str && str.equals("")) {
			return false;
		}
		return true;
	}

	/**
	 * 计算折扣
	 * 
	 * @param str
	 * @return
	 */
	public static String getDiscount(String str) {
		BigDecimal b = new BigDecimal(str);
		b = b.multiply(new BigDecimal(10));
		float result = b.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
		return String.valueOf(result);
	}

	/**
	 * 判断给定字符串是否空白串�???空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
	 * 
	 * @param input
	 * @return boolean
	 */
	public static boolean isEmpty(String input) {
		if (input == null || "".equals(input) || "null".equals(input))
			return true;

		for (int i = 0; i < input.length(); i++) {
			char c = input.charAt(i);
			if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
				return false;
			}
		}
		return true;
	}

	/**
	 * 获取时间�???
	 * 
	 * @param oldTime
	 * @return long
	 */
	@SuppressLint("SimpleDateFormat")
	public static long getLongTime(String oldTime) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		long result = 0;
		try {
			date = format.parse(oldTime);
			result = date.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}

	// 日期控件
	private Dialog dateDialog = null;
	private DatePicker datePicker;// 日期选择控件
	private Button dateOkBtn;// 确定日期选择
	@SuppressWarnings("unused")
	private Date date;

	/**
	 * 选择日期
	 * 
	 * @param context
	 */
	public void showDateDialog(Context context, final TextView editText) {
		if (dateDialog == null) {
			dateDialog = new Dialog(context, R.style.gbfdialogstyle);
			dateDialog.setCanceledOnTouchOutside(true);
			dateDialog.setContentView(R.layout.dialog_date_picker);
			dateOkBtn = (Button) dateDialog
					.findViewById(R.id.dialog_date_ok_btn);
			datePicker = (DatePicker) dateDialog
					.findViewById(R.id.dialog_date_datepicker);

			date = new Date();
			dateOkBtn.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View arg0) {
					datePicker.clearFocus();
					int m = datePicker.getMonth() + 1;
					int d = datePicker.getDayOfMonth();
					editText.setText(datePicker.getYear() + "-"
							+ (m > 9 ? m : ("0" + m)) + "-"
							+ (d > 9 ? d : ("0" + d)));
					datePicker.setVisibility(View.GONE);
					dateDialog.dismiss();
				}
			});

		}
		datePicker.setVisibility(View.VISIBLE);
		dateDialog.show();
		// 设置弹出窗位
		dateDialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
	}

	public void showDateDialog1(Context context, final TextView editText) {
		if (dateDialog == null) {
			dateDialog = new Dialog(context, R.style.gbfdialogstyle);
			dateDialog.setCanceledOnTouchOutside(true);
			dateDialog.setContentView(R.layout.dialog_date_picker);
			dateOkBtn = (Button) dateDialog
					.findViewById(R.id.dialog_date_ok_btn);
			datePicker = (DatePicker) dateDialog
					.findViewById(R.id.dialog_date_datepicker);

			date = new Date();
			dateOkBtn.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View arg0) {
					datePicker.clearFocus();
					int m = datePicker.getMonth() + 1;
					int d = datePicker.getDayOfMonth();
					editText.setText(datePicker.getYear() + "�??"
							+ (m > 9 ? m : ("0" + m)) + "�??"
							+ (d > 9 ? d : ("0" + d)) + "�??");
					datePicker.setVisibility(View.GONE);
					dateDialog.dismiss();
				}
			});

		}
		datePicker.setVisibility(View.VISIBLE);
		dateDialog.show();
		// 设置弹出窗位
		dateDialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
	}

	public static String FixString(String src, int len, boolean isAddDel) {
		if (src.length() <= len)
			return src;
		return src.substring(0, len) + (isAddDel ? "..." : "");
	}

	public static String GetUrlDomain(String url) {
		int startpos = url.indexOf("://") + 3;
		int endpos = url.substring(startpos).indexOf("/");
		return url.substring(startpos).substring(0, endpos);
	}

	public static String FixTime(long time) {
		long n = System.currentTimeMillis() - time;
		if (n < (long) 1000l * 60) {
			return "1分钟�?";
		} else if (n < (long) 1000l * 60 * 60) {
			long min = n / (long) (1000l * 60);
			return min + "分钟�?";
		} else if (n < (long) 1000l * 60 * 60 * 24) {
			long hour = n / (long) (1000l * 60 * 60);
			return hour + "小时�?";
		} else if (n < (long) 1000l * 60 * 60 * 24 * 30) {
			long day = n / (long) (1000l * 60 * 60 * 24);
			return day + "天前";
		}
		// else if (n < (long) 1000l * 60 * 60 * 24 * 30 * 12)
		// {
		// long month = n / (long) (1000l * 60 * 60 * 24 * 30);
		// return month + "月前";
		// }
		else {
			return "";
		}
	}

	public static String FixTime2(long time) {
		long n = time - System.currentTimeMillis();
		if (n > (long) (1000l * 60 * 60 * 24)) {
			long day = n / (long) (1000l * 60 * 60 * 24);
			return day + "天后";
		} else if (n > (long) (1000l * 60 * 60)) {
			long hour = n / (long) (1000l * 60 * 60);
			return hour + "小时�?";
		} else if (n > (long) (1000l * 60)) {
			long min = n / (long) (1000l * 60);
			return min + "分钟�?";
		}
		return "已过�?";
	}

	public static String FixTime4(long time) {
		long n = time - System.currentTimeMillis();
		if (n <= (long) (1000l * 60 * 60 * 24 * 7)) {
			long day = n / (long) (1000l * 60 * 60 * 24);
			return day + "";
		}
		return null;
	}

	public static String FixTime3(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm");
		java.util.Date dt = new Date(time);
		String sDateTime = sdf.format(dt); // 得到精确到秒的表示：08/31/2006 21:08:00
		return sDateTime;
	}

	public static String FixWords(int words) {
		if (words > 10000) {
			return (words / 10000) + "万字";
		}
		return words + "�?";
	}

	public static String fixStr(String str) {
		String[] removeStr = { "\r", "<br>", "�?", "</br>", "\r<br>" };
		return trans(str, removeStr);

	}

	private static String trans(String str, String[] removeStr) {
		for (String rs : removeStr) {
			str = str.replaceAll(rs, "");
		}
		// System.out.println("str==========="+str);
		return str;

	}

	public static String fixNewStr(String str) {
		String[] reStr = { "&nbsp;", " " };
		for (String rs : reStr) {
			str = str.replaceAll(rs, "");
		}
		return str;
	}

	public static String nbspToSpace(String str) {
		String[] reStr = { "&nbsp;" };
		for (String rs : reStr) {
			str = str.replaceAll(rs, " ");
		}
		return str;
	}

	public static String splitStr(String str) {
		if (str.length() > 80)
			return str.substring(0, 80);
		else
			return str;
	}

	public static String getTime(long l) {
		SimpleDateFormat sDateFormat = new SimpleDateFormat("MM-dd");
		Date d = new Date(l);
		return sDateFormat.format(d);

	}

	public static String fixTime(long l) {
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date d = new Date(l);
		return sDateFormat.format(d);

	}

	public static String fixTime01(long l) {
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date d = new Date(l);
		return sDateFormat.format(d);

	}

	public static String formatDate(Date mDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(mDate);
	}

	/**
	 * 查找字符串中包含了另�?个字符串几次
	 * 
	 * @param s
	 * @param toFind
	 * @return
	 */
	public static int findStrContainsCount(String s, String toFind) {
		int index = 0;
		int count = 0;
		while (index != -1) {
			index = s.indexOf(toFind);
			if (index == -1)
				break;
			int length = toFind.length();
			s = s.substring(index + length);
			count++;
		}
		return count;
	}
}
